<?php
namespace Muyuxuan\ThreePayment\three\bct;
use Muyuxuan\ThreePayment\AgentInterface;
use Muyuxuan\ThreePayment\three\bct\tool\SecurityUtil\NewRsaUtil;
use Muyuxuan\ThreePayment\three\bct\tool\Util\HttpClient;
use Muyuxuan\ThreePayment\three\bct\tool\Util\Tools;

class Agent implements AgentInterface
{
    use \Muyuxuan\ThreePayment\tools\ConfigTrait;
    protected $config = [
        'version'=>'4.1.0',
    ];
    /**
     * 添加网络接入
     * @param int|string $accType 入网类型 1个人 2商户/个体
     * @param array $accInfo 入网信息所用数组
     * @param string $noticeUrl 异步通知地址
     * @param string $businessType 业务类型:bct=BCT2.0
     * @return mixed
     */
    public function addNetworkAccess($accType,$accInfo,$noticeUrl='',$businessType="",$event=[]){
        if(empty($businessType)){
            $businessType = "BCT2.0";
        }
        if(!in_array($accType,[1,2])){
            return $this->error("入网类型错误");
        }
        if(!is_array($accInfo)){
            return $this->error("入网信息错误");
        }
        if(empty($accInfo['transSerialNo'])){
            $accInfo['transSerialNo'] = Tools::getTsnOderid("TSN");
        }
        if(empty($accInfo['loginNo'])){
            $accInfo['loginNo'] = Tools::getTransid("LN");
        }
        if(empty($accInfo['platformTerminalId'])){
            $accInfo['platformTerminalId'] = $this->config['terminal_id'];
        }
        $HeaderPost = [
            'memberId'=>$this->config['agent_id'],
            'terminalId'=>$this->config['terminal_id'],
            'serviceTp'=>'T-1001-013-01',
            'verifyType'=>'1',
        ];
        $newData = [
            'version'=>$this->config['version'],
            'accType'=>$accType,
            'accInfo'=>$accInfo,
            'noticeUrl'=>$noticeUrl,
            'businessType'=>$businessType,
        ];
        $newData = array_merge($newData,$event);
        $allData = [
            'header'=>$HeaderPost,
            'body'=>$newData
        ];
        $res = $this->post($allData);
        echo '<pre>';
        print_r($res);
        exit;
//        $HeaderPost['content'] = $this->getContent($allData);
//        $PostString  =  HttpClient::Post($HeaderPost, Tools::getUrl($HeaderPost["serviceTp"],$this->isTest));
//        if(empty($PostString)){
//            return $this->error("返回异常！");
//        }
//        $RPostString = NewRsaUtil::decrptByPub($PostString,$this->config['cer_file_path']);
//        if(is_string($RPostString)){
//            return json_decode($RPostString,true);
//        }
    }
    protected function getContent($data)
    {
        $JsonObject = json_encode($data);
        return NewRsaUtil::encryptByPfx($JsonObject,$this->config['pfx_file_path'],$this->config['key_pwd']);
    }
    protected function post($allData)
    {
        $HeaderPost = $allData['header'];
        $HeaderPost['content'] = $this->getContent($allData);
//        echo '<pre>';
//        print_r($HeaderPost);
//        exit;
        $PostString  =  HttpClient::Post($HeaderPost, Tools::getUrl($HeaderPost["serviceTp"],$this->isTest));
        if(empty($PostString)){
            return $this->error("返回异常！");
        }
        $PostArray = json_decode($PostString,true);
        if(empty($PostArray)){
            $RPostString = NewRsaUtil::decrptByPub($PostString,$this->config['cer_file_path']);
            if(is_string($RPostString)){
                return json_decode($RPostString,true);
            }
            return $this->error("返回异常！");
        }else{
            return $PostArray;
        }
//        return $this->error("返回异常！");
    }
}